% To calculate transition prob & its standardized one
clear all;
clc;
disp('To calculate qtrly transition prob & its standardized one');
load TP06_Q_RJ_fin_R5.mat
%load 97_09_final_Q.mat 
freqj=4;% 4 quarters
year_list=[1997:2009]';
year_list_text=num2str(year_list);
quarter_list=[1:4]';
quarter_list_text=num2str(quarter_list);
Rate_list=[0,1:0.5:5];
prob=zeros(13,freqj,10,12);% the last column will be 1, so we ignore the 13th column and set it as 12 columns
prob_std=zeros(13,freqj,10,12); % the 12th column is useless, we define 12 column for coding conveinence.
filename1='TP06_Q_RJ_fin_R5_transition prob.xls';
filename2='TP06_Q_RJ_fin_R5_transition prob_std.xls';

% filename1='transition prob.xls';
% filename2='transition prob_std.xls';

for i=1:13
    for j=1:freqj
        for ii=1:length(Rate_list)
            for jj=1:12
                prob(i,j,ii,jj) = final_result(i,j,ii,jj)/final_result(i,j,ii,13);
            end
        end
        xx(:,:)=prob(i,j,:,:);
        sheetlist=strcat('',year_list_text(i,:),'.',quarter_list_text(j,:),'');
        xlswrite(filename1, xx, sheetlist);
        clear xx sheetlist
    end
end

for i=1:13
    for j=1:freqj
        for ii=1:length(Rate_list)
            for jj=1:12
                prob_std(i,j,ii,jj) = prob(i,j,ii,jj)/(1-prob(i,j,ii,12));
            end
        end
        xx(:,:)=prob_std(i,j,:,:);
        sheetlist=strcat('',year_list_text(i,:),'.',quarter_list_text(j,:),'');
        xlswrite(filename2, xx, sheetlist);
        clear xx sheetlist
    end
end
